package com.teacher.pc.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.teacher.common.CommonResult;
import com.teacher.mapper.TermMapper;
import com.teacher.pojo.dto.punishment.PunishmentAddDto;
import com.teacher.pojo.dto.punishment.PunishmentGetDto;
import com.teacher.pojo.dto.punishment.PunishmentUpdateDto;
import com.teacher.pojo.entity.Punishment;
import com.teacher.pojo.vo.punishment.PunishmentGetVo;
import com.teacher.service.IPunishmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 * 违纪处分 前端控制器
 * </p>
 *
 * @author zfm
 * @since 2021-08-01
 */
@RestController
@RequestMapping("/pc/punishment")
@Api(tags = "违纪处分")
public class PunishmentController {
    @Autowired
    private IPunishmentService iPunishmentService;

    @Autowired
    private TermMapper termMapper;

    /**
     * 添加违纪处分
     *
     * @param dto
     * @return
     */
    @ApiOperation("添加违纪处分 punishment_add 10->留校查看处分 20->记过处分 30->严重警告处分 40->警告处分 50->通报批评")
    @PostMapping
    @PreAuthorize("hasAuthority('punishment_add')")
    public CommonResult<String> add(@RequestBody @Validated PunishmentAddDto dto) {
        Punishment punishment = new Punishment();
        dto.setTermId(termMapper.getCurrentTerm());
        BeanUtils.copyProperties(dto, punishment);
        if (iPunishmentService.save(punishment)) {
            return CommonResult.success("添加成功");
        }
        return CommonResult.failed("添加失败");
    }

    /**
     * 更新违纪处分
     *
     * @param dto
     * @return
     */
    @ApiOperation("更新违纪处分 punishment_update")
    @PutMapping
    @PreAuthorize("hasAuthority('punishment_update')")
    public CommonResult<String> update(@RequestBody @Validated PunishmentUpdateDto dto) {
        Punishment punishment = new Punishment();
        BeanUtils.copyProperties(dto, punishment);
        if (iPunishmentService.updateById(punishment)) {
            return CommonResult.success("更新成功");
        }
        return CommonResult.failed("更新失败");
    }

    /**
     * 删除违纪处分
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("删除违纪处分 punishment_delete")
    @PreAuthorize("hasAuthority('punishment_delete')")
    public CommonResult<String> del(@RequestBody List<Long> ids) {
        if (iPunishmentService.removeByIds(ids)) {
            return CommonResult.success("删除成功");
        }
        return CommonResult.failed("删除失败");
    }

    /**
     * 查询违纪处分
     *
     * @param dto
     * @return
     */
    @GetMapping
    @ApiOperation("查询违纪处分 punishment_get")
    @PreAuthorize("hasAuthority('punishment_get')")
    public CommonResult<IPage<PunishmentGetVo>> getPage(PunishmentGetDto dto) {
        return iPunishmentService.getPage(dto);
    }

    /**
     * 导入违纪处分
     *
     * @param file
     * @return
     */
    @PostMapping("/upload")
    @ApiOperation("导入违纪处分 punishment_upload")
    @PreAuthorize("hasAuthority('punishment_upload')")
    public CommonResult<String> upload(@RequestParam(name = "file") MultipartFile file) {
        return iPunishmentService.upload(file);
    }
}

